$(function(){
	initSearchBox();
	initSearchButton();
	initBlockUI();
	// $("#searchresult_block").show();
	// updateTable(0, $("#.pagination"));
});

// 初始化搜索框
function initSearchBox()
{
	$("#searchstr")
		.addClass("searchstrbg")
		.focus(function(){
			$(this).removeClass("searchstrbg")
			$(this).select();
		})
		.blur(function(){
			if ($.trim($(this).val()) == "")
			{
				$(this).addClass("searchstrbg");
			}
		});
}

// 初始化“搜索”按钮
function initSearchButton()
{
	// 每次搜索置当前页为首页
	// $("#current_page").val(0);
	$("#searchit").click(function(){
		updateTable(0, $("#.pagination"));
		// myPager();
		$("#searchnotice").show();
		$("#searchresult_block").show();
	});
}

// 分页
function myPager(page)
{
	var items_num = $("#items_num").val();
	$(".pagination").pagination(items_num, {
		current_page: page,
		prev_text: "前一页",
		next_text: "后一页",
		callback: updateTable
	});
}

// 更新数据表格，分页的回调函数
function updateTable(page, jq)
{
	// alert(page);
	// 当前页置为 page
	$("#current_page").val(page);
	
	var url = $("#searchform").attr("action");
	var pars = {
		page_status:		1,
		current_page:		page,
		searchstr:		$("#searchstr").val(),
		instancy:			$("#instancy").val(),
		finishedflag:		$("#finishedflag").val(),
		passedflag:		$("#passedflag").val(),
		accessoryflag:		$("#accessoryflag").val(),
		applydate:		$("#applydate").val()
	};
	$.post(url, pars, function(xmlobj){
		var whole = $(xmlobj).find("docs").attr("whole");
		var sstr = $("#searchstr").val();
		$("#items_num").val(whole);
		$("#searchnotice").html("有 <span class=\"strong\">" + whole + "</span> 项符合“<span class=\"strong\">" + sstr + "</span>”的搜索结果：");
		$("#tbody2").html("");
		$(xmlobj).find("item").each(function(i){
			var docid		= $(this).find("docid").text();
			var title			= $(this).find("title").text();
			var instancy		= $(this).find("instancy").text();
			var applytime	= $(this).find("applytime").text().split(" ")[0];
			var ismulti		= $(this).find("ismulti").text();
			var issent		= $(this).find("issent").text();
			var iscanceled	= $(this).find("iscanceled").text();
			var isfinished	= $(this).find("isfinished").text();
			var ispassed		= $(this).find("ispassed").text();
			var status		= "" + getDocStatusBlock(ismulti, 0)
					 + getDocStatusBlock(issent, 1)
					 + getDocStatusBlock(iscanceled, 2)
					 + getDocStatusBlock(isfinished, 3)
					 + getDocStatusBlock(ispassed, 4) + "<br />";
			$("<tr></tr>")
				.append($("<th></th>").html(i + 1))
				.append($("<td></td>").html(title))
				.append($("<td></td>").html(instancy))
				.append($("<td></td>").html(applytime))
				.append($("<td></td>").html(status))
				.append($("<td></td>").html(
					$("<a href=\"#\"></a>")
						.attr("docid", docid)
						.html("详细信息")
						.click(function(){
							showInfo(this, $(this).attr("docid"));
							return false;
						})
				))
				.appendTo("#tbody2");
			
			$("a.doc_status_tip0, a.doc_status_tip1").tooltip({
				delay: 0,
				// track: true,
				showURL: false,
				bodyHandler: function(){ var title = $(this).attr("linktitle"); return $("<div></div>").html(title); }
			});
			
			tablePretty("#tbody2");
			myPager(page);
		});
	});
	
	return false;
}

// 显示详细信息
function showInfo(block, docid)
{
	var title = $(block).parent().prev().prev().prev().prev().html();
	var instancy = $(block).parent().prev().prev().prev().html();
	var applytime = $(block).parent().prev().prev().html();
	var status = $(block).parent().prev().html();
	
	var url = "doc_inquiry.php";
	var pars = {
		page_status: 2,
		doc_id: docid
	};
	$.post(url, pars, function(xmlobj){
		var content = $(xmlobj).find("content").text().unescapeString();
		$("#di_title").html(title);
		$("#di_content").html(content);
		$("#di_instancy").html(instancy);
		$("#di_applytime").html(applytime);
		$("#di_status").html(status);
		
		$("#detailed_info").modal({
			onShow: function() {
				$("#searchform input, #searchform select").not("[@type='hidden']").css("visibility", "hidden");
			},
			onClose: function() {
				$("#searchform input, #searchform select").not("[@type='hidden']").css("visibility", "visible");
				$.modal.close();
			}
		});
		/*
		$("#di_status a.doc_status_tip0, #di_status a.doc_status_tip1").tooltip({
			delay: 0,
			showURL: false,
			bodyHandler: function() { var title = $(this).attr("linktitle"); return $("<div></div>").html(title); }
		});
		*/
	});
}